#include "thumbnailview.h"
#include <QIcon>
#include <QPixmap>
#include <QApplication>
#include <QDebug>
ThumbnailView::ThumbnailView(QWidget *parent) : QListWidget(parent)
    , m_isPDFMode(false)
    , m_totalPDFPages(0)
    , m_lastSelectedIndex(-1)
{
    setViewMode(QListWidget::IconMode);
    setIconSize(QSize(120, 160));  // 增加缩略图尺寸
    setSpacing(15);                 // 增加间距
    setMovement(QListWidget::Static);
    setResizeMode(QListWidget::Adjust);
    // 设置居中对齐
    setItemAlignment(Qt::AlignCenter);
    // 隐藏水平滚动条
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // 设置专业的高亮样式，与主视图保持一致
    setStyleSheet(R"(
        QListWidget::item {
            border: 2px solid transparent;
            border-radius: 5px;
            padding: 5px;
            margin: 2px;
            background-color: transparent;
            color: black;
        }
        QListWidget::item:selected {
            border: 3px solid rgb(0, 122, 255);
            background-color: rgba(0, 122, 255, 40);
            color: black;
            font-weight: bold;
        }
        QListWidget::item:hover {
            border: 2px solid rgba(0, 122, 255, 120);
            background-color: rgba(0, 122, 255, 20);
            color: black;
        }
        QListWidget::item:selected:hover {
            border: 3px solid rgb(0, 122, 255);
            background-color: rgba(0, 122, 255, 60);
            color: black;
            font-weight: bold;
        }
    )");
    connect(this, &QListWidget::currentRowChanged, this, [this](int row) {
        // 只有当选中的索引发生变化时才发射信号，防止重复点击同一索引导致页面重复刷新
        if (row != m_lastSelectedIndex) {
            m_lastSelectedIndex = row;
            emit pageSelected(row);
        } else {
        }
    });
}
void ThumbnailView::setPages(const QList<QImage>& pages)
{
    clear();  // 清除所有項目
    m_lastSelectedIndex = -1;  // 重置选中索引
    m_pages = pages;
    for (int i = 0; i < pages.size(); ++i) {
        // 檢查圖像有效性
        if (pages[i].isNull() || pages[i].width() <= 0 || pages[i].height() <= 0) {
            continue;
        }
        // 創建縮略圖
        createThumbnail(pages[i], i);
        // 每处理10个缩略图就处理一次事件，避免UI阻塞
        if (i % 10 == 9) {
            QApplication::processEvents();
        }
    }
}
void ThumbnailView::createThumbnail(const QImage& image, int index)
{
    try {
        // 檢查圖標大小
        QSize targetSize = iconSize();
        if (targetSize.width() <= 0 || targetSize.height() <= 0) {
            targetSize = QSize(120, 160);  // 默認大小
        }
        // 創建縮略圖 - 先轉換為QPixmap
        QPixmap pixmap = QPixmap::fromImage(image);
        if (pixmap.isNull()) {
            return;
        }
        QPixmap thumbnail = pixmap.scaled(targetSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        if (thumbnail.isNull()) {
            return;
        }
        // 創建列表項
        QListWidgetItem* item = new QListWidgetItem(this);
        if (!item) {
            return;
        }
        item->setIcon(QIcon(thumbnail));
        item->setText(tr("第 %1 页").arg(index + 1));
        item->setTextAlignment(Qt::AlignCenter);
        // 設置合適的大小
        item->setSizeHint(QSize(targetSize.width() + 20,
                               targetSize.height() + 30));  // 為文字預留空間
        addItem(item);
    } catch (const std::exception& e) {
    } catch (...) {
    }
}
void ThumbnailView::setCurrentPage(int index)
{
    if (index >= 0 && index < count()) {
        m_lastSelectedIndex = index;  // 更新记录的索引
        setCurrentRow(index);
    }
}
void ThumbnailView::retranslateUi()
{
    // 更新所有缩略图的页码文本
    for (int i = 0; i < count(); ++i) {
        QListWidgetItem* item = this->item(i);
        if (item) {
            item->setText(tr("第 %1 页").arg(i + 1));
        }
    }
}